/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package topcoder;

/**
 *
 * @author Mohamed.Azouz
 */
public class MinimalDifference {

    public int findNumber(int A, int B, int C) {
        int X = A;
        int c = this.getdigitSum(C);
        int mindif = Math.abs(c - this.getdigitSum(A));
        System.out.println(X +  " " + mindif);
        for (int i = A + 1; i <= B; i++) {
            int x = this.getdigitSum(i);
            System.out.println(i +  " " + Math.abs(c - x));
            if (Math.abs(c - x) < mindif) {
                X = i;
                mindif=Math.abs(c - x);
            }
        }
        return X;
    }

    private int getdigitSum(int x) {
        String xStr = String.valueOf(x);
        int tot = 0;
        for (int i = 0; i < xStr.length(); i++) {
            tot += Integer.valueOf(xStr.charAt(i) + "");
        }
        return tot;
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        MinimalDifference md = new MinimalDifference();
        int a = 1987;
        int b = 9123;
        int c = 1;
        System.out.println(md.findNumber(a, b, c));
    }
}
